<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\SettingRequest;
use App\Http\Requests\Admin\UserRoleRequest;
use App\Models\OriginalModels\User;
use App\Services\SettingService;
use App\Traits\Admin\SuccessOrFailure;
use Daijulong\LaravelRoles\Permission;
use Illuminate\Http\Request;

class SettingController extends Controller
{

    use SuccessOrFailure;

    private $service;

    public function __construct(SettingService $service)
    {
        $this->service = $service;
    }

    public function showSetForm($group = 0)
    {

        $this->canOrFailure('setting.set');
        $settings = $this->service->getByGroup($group);
        $groups = $this->service->getGroups();
        return view('admin.setting.set', compact('settings', 'groups', 'group'));
    }

    public function saveSetting(Request $request)
    {
        $this->canOrFailure('setting.set');
        $this->service->setting($request->get('setting') ?: []);
        return $this->success();
    }

    public function index(Request $request)
    {
        $this->canOrFailure('setting.param');
        $condition = [];
        if ($group = $request->get('group')) {
            $condition['group'] = $group;
        }
        if ($name = $request->get('name')) {
            $condition['name'] = $name;
        }
        if ($title = $request->get('title')) {
            $condition['title'] = $title;
        }
        $groups = $this->service->getGroups();
        $types = $this->service->getTypes();
        $settings = $this->service->paginate($condition);
        return view('admin.setting.index', compact('groups', 'types', 'settings'));
    }

    public function create()
    {
        $this->canOrFailure('setting.param');
        $groups = $this->service->getGroups();
        $types = $this->service->getTypes();
        return view('admin.setting.create', compact('groups', 'types'));
    }

    public function store(SettingRequest $request)
    {
        $this->canOrFailure('setting.param');
        $attributes = [
            'title' => $request->get('title'),
            'name' => $request->get('name'),
            'value' => $request->get('value'),
            'group' => $request->get('group'),
            'type' => $request->get('type'),
            'addon_options' => $request->get('addon_options'),
            'sort' => $request->get('sort'),
            'description' => $request->get('description'),
        ];
        $this->service->create($attributes);
        return $this->success('', route('admin.setting.index'));
    }

    public function edit($id)
    {
        $this->canOrFailure('setting.param');
        $setting = $this->getSettingOrFailure($id);
        $groups = $this->service->getGroups();
        $types = $this->service->getTypes();
        return view('admin.setting.edit', compact('setting', 'groups', 'types'));
    }

    public function update(SettingRequest $request, $id)
    {
        $this->canOrFailure('setting.param');
        $setting = $this->getSettingOrFailure($id);
        $attributes = [
            'title' => $request->get('title'),
            'value' => $request->get('value'),
            'group' => $request->get('group'),
            'type' => $request->get('type'),
            'addon_options' => $request->get('addon_options'),
            'sort' => $request->get('sort'),
            'description' => $request->get('description'),
        ];
        $this->service->update($setting, $attributes);
        return $this->success('', $request->get('back_url') ?: route('admin.role.user.index'));
    }

    public function destroy($id)
    {
        $this->canOrFailure('setting.param');
        $setting = $this->getSettingOrFailure($id);
        $this->service->destroy($setting);
        return $this->success();
    }


    public function refresh()
    {
        $this->service->cache();
        return $this->success();
    }

    private function getSettingOrFailure($id)
    {
        $setting = $this->service->getById($id);
        if (!$setting) {
            response($this->failure('配置不存在！'), 404)->send();
            exit;
        }
        return $setting;
    }
}
